package com.bdqn.appjob.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.bdqn.appjob.entity.DevUser;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class DevUserController {
	private Logger logger = Logger.getLogger(DevUserController.class);
	//@Resource
	//private DevUserService devUserService;
	//403权限
	@RequestMapping("/403")
	public String unauthorizedRele(){
		System.out.println("----------没有权限充钱吧----------");
		return "403";
	}
	//系统入口
   @RequestMapping(value="/index")
   public String indexs() {
	   return "index";	   
   }
   //进入APP开发者平台--登录页面
   @RequestMapping(value="/devlogin.html")
   public String devLogin() {
	   return "devlogin";
   }
   //验证登录是否成功
//   @RequestMapping(value="/selectLogin")
//   public String LoginSuccess(@RequestParam String devCode,@RequestParam String devPassword
//		   ,HttpSession session,HttpServletRequest request) {
//	   logger.info("login=================");
//	   logger.info("devCode"+devCode+"devPassword:"+devPassword);
//   	   DevUser devUser=null;
//   	   devUser=devUserService.selectDevLogin(devCode,devPassword);
//	   System.out.println("dev进来啦");
//   	   if(devUser != null) {
//   		   logger.debug("登入成功====="+devUser.getDevName());
//   		session.setAttribute("devUserSession", devUser);
//   		return "developer/main";
//   	   }
//	   return "devlogin";	   //否则，还是进入登录页面
//   }

   @RequestMapping("/selectLogin")
	public String login(@RequestParam String devCode, @RequestParam String devPassword, Model model, HttpSession session){
	   //获取当期的用户
	   Subject subject = SecurityUtils.getSubject();
	   //封装用户的登陆数据
	   UsernamePasswordToken token = new UsernamePasswordToken(devCode,devPassword);
	   try {
		   subject.login(token);		//执行登陆方法,如果没有异常就说明ok了
		   DevUser devUser = (DevUser)SecurityUtils.getSubject().getPrincipal();
		   System.out.println("shiro登陆成功了");
		   session.setAttribute("devUserSession",devUser);
		   return "developer/main";
	   }catch (UnknownAccountException e){		//用户名不存在
		   model.addAttribute("msg","用户名错误");
		   return "devlogin";
	   }catch (IncorrectCredentialsException e){		//密码不存在
		   model.addAttribute("msg","密码不存在");
		   return "devlogin";
	   }
   }
}
